package gui.gestion.forms;

import java.awt.Dimension;
import java.awt.GridLayout;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.TitledBorder;
import objetosNegocio.Familia;

public class FamilForm extends JPanel{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JTextField idFamilia;
	private JTextField categoria;
	private JTextField subcategoria;
	private JCheckBox activo;
	
	/**
	 * Default constructor
	 */
	public FamilForm(){
		this.setBorder(new TitledBorder("Familia"));
		this.setLayout(new GridLayout(4,2));
		this.setMinimumSize(new Dimension(350,180));
		this.setPreferredSize(new Dimension(350,180));
		buildFamilForm();
		
	}
	
	/**
	 * Builds the form
	 */
	private void buildFamilForm() {
	
		JLabel l = new JLabel("ID", JLabel.TRAILING);
        this.add(l);
        idFamilia = new JTextField(10);
        idFamilia.setEditable(false); //No debe poder ser modificado
        l.setLabelFor(idFamilia);
        this.add(idFamilia);
        
		JLabel l2 = new JLabel("Categoria", JLabel.TRAILING);
        this.add(l2);
        categoria = new JTextField(30);
        l2.setLabelFor(categoria);
        this.add(categoria); 
        
		JLabel l3 = new JLabel("Activo ?", JLabel.TRAILING);
        this.add(l3);
        activo = new JCheckBox();
        l3.setLabelFor(activo);
        this.add(activo);
        
        JLabel l4 = new JLabel("Subcategoria", JLabel.TRAILING);
        this.add(l4);
        subcategoria = new JTextField(30);     
        l4.setLabelFor(subcategoria);
        this.add(subcategoria);
		
	}
	
	/**
	 * Fills the form with family info
	 * @param cli
	 */
	public void fillFamilyForm(Familia fam){
		this.activo.setSelected(fam.getActivo());
		this.categoria.setText(fam.getCategoria());
		this.idFamilia.setText(Integer.toString(fam.getIdFamilia()));
		this.subcategoria.setText(fam.getSubcategoria());
	}
	
	/**
	 * Retrieves a Family object with the info from the form
	 * @return
	 */
	public Familia getFamilyObject(){
		Familia ret = new Familia();
		if(this.activo.getSelectedObjects() != null)	ret.setActivo(true);
		else												ret.setActivo(false);
		
		try{
			ret.setIdFamilia(Integer.parseInt(this.idFamilia.getText()));	
		}catch (NumberFormatException e){}
		
		ret.setCategoria(this.categoria.getText());
		ret.setSubcategoria(this.subcategoria.getText());
        return ret;
	}
		

}
